package com.example.victory.utils;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class MoneyHelper {
    /**
     * 格式化金额
     *
     * @param money 需要格式的金额
     * @return 被处理后的字符串
     */
    public static String formatMoney(BigDecimal money) {
        String unit = "元";
        BigDecimal TEN_THOUSAND = new BigDecimal("10000");
        BigDecimal ONE_HUNDRED_MILLION = new BigDecimal("100000000");
        String result = "" + money;
        // 保留两位小数
        DecimalFormat formater = new DecimalFormat("0.00");
        formater.setRoundingMode(RoundingMode.HALF_UP);
        if (money == null) return "0";
        if (money.compareTo(BigDecimal.ZERO) == -1) {
            BigDecimal meneyEngative = money.abs();
            if (meneyEngative.compareTo(BigDecimal.ZERO) == 0) {
                result = "0";
            } else if (meneyEngative.compareTo(TEN_THOUSAND) == -1) {
                // 小于10000，只做保留2位小数处理
                result = formater.format(meneyEngative);
            } else if (meneyEngative.compareTo(TEN_THOUSAND) == 0 || meneyEngative.compareTo(ONE_HUNDRED_MILLION) == -1) {
                unit = "万";
                BigDecimal divide = meneyEngative.divide(TEN_THOUSAND);
                result = formater.format(divide) + unit;
            } else if (meneyEngative.compareTo(ONE_HUNDRED_MILLION) == 0 || meneyEngative.compareTo(ONE_HUNDRED_MILLION) == 1) {
                unit = "亿";
                BigDecimal divide = meneyEngative.divide(ONE_HUNDRED_MILLION);
                result = formater.format(divide) + unit;
            }
            return formatMoney("-" + result);
        } else {
            if (money.compareTo(BigDecimal.ZERO) == 0) {
                result = "0";
            } else if (money.compareTo(TEN_THOUSAND) == -1) {
                // 小于10000，只做保留2位小数处理
                result = formater.format(money);
            } else if (money.compareTo(TEN_THOUSAND) == 0 || money.compareTo(ONE_HUNDRED_MILLION) == -1) {
                unit = "万";
                BigDecimal divide = money.divide(TEN_THOUSAND);
                result = formater.format(divide) + unit;
            } else if (money.compareTo(ONE_HUNDRED_MILLION) == 0 || money.compareTo(ONE_HUNDRED_MILLION) == 1) {
                unit = "亿";
                BigDecimal divide = money.divide(ONE_HUNDRED_MILLION);
                result = formater.format(divide) + unit;
            }
            return formatMoney(result);
        }
    }

    private static String formatMoney(String money) {
        if (StringUtils.isEmpty(money)) return "0";
        return money.replace(".00", "");
    }

    public static class Asset {
        String value;
        String unit;

        public void setValue(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }

        public void setUnit(String unit) {
            this.unit = unit;
        }

        public String getUnit() {
            return unit;
        }
    }


    /**
     * 格式化金额
     *
     * @param money 需要格式的金额
     * @return 被处理后的字符串
     */
    public static Asset formatMoneyWithUnit(BigDecimal money) {
        Asset asset = new Asset();
        String unit = "元";
        BigDecimal TEN_THOUSAND = new BigDecimal("10000");
        BigDecimal ONE_HUNDRED_MILLION = new BigDecimal("100000000");
        String result = "" + money;
        // 保留两位小数
        DecimalFormat formater = new DecimalFormat("0.00");
        formater.setRoundingMode(RoundingMode.HALF_UP);
        if (money == null) {
            asset.setValue("0");
            asset.setUnit(unit);
            return asset;
        }
        if (money.compareTo(BigDecimal.ZERO) == -1) {
            BigDecimal meneyEngative = money.abs();
            if (meneyEngative.compareTo(BigDecimal.ZERO) == 0) {
                result = "0";
            } else if (meneyEngative.compareTo(TEN_THOUSAND) == -1) {
                // 小于10000，只做保留2位小数处理
                result = formater.format(meneyEngative);
            } else if (meneyEngative.compareTo(TEN_THOUSAND) == 0 || meneyEngative.compareTo(ONE_HUNDRED_MILLION) == -1) {
                unit = "万";
                BigDecimal divide = meneyEngative.divide(TEN_THOUSAND);
                result = formater.format(divide);
            } else if (meneyEngative.compareTo(ONE_HUNDRED_MILLION) == 0 || meneyEngative.compareTo(ONE_HUNDRED_MILLION) == 1) {
                unit = "亿";
                BigDecimal divide = meneyEngative.divide(ONE_HUNDRED_MILLION);
                result = formater.format(divide);
            }
            asset.setValue(formatMoney("-" + result));
            asset.setUnit(unit);
            return asset;
        } else {
            if (money.compareTo(BigDecimal.ZERO) == 0) {
                result = "0";
            } else if (money.compareTo(TEN_THOUSAND) == -1) {
                // 小于10000，只做保留2位小数处理
                result = formater.format(money);
            } else if (money.compareTo(TEN_THOUSAND) == 0 || money.compareTo(ONE_HUNDRED_MILLION) == -1) {
                unit = "万";
                BigDecimal divide = money.divide(TEN_THOUSAND);
                result = formater.format(divide);
            } else if (money.compareTo(ONE_HUNDRED_MILLION) == 0 || money.compareTo(ONE_HUNDRED_MILLION) == 1) {
                unit = "亿";
                BigDecimal divide = money.divide(ONE_HUNDRED_MILLION);
                result = formater.format(divide);
            }
            asset.setValue(formatMoney(result));
            asset.setUnit(unit);
            return asset;
        }
    }

}
